package com.moyou.weichat.chat;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.util.DateUtils;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.moyou.weichat.R;
import com.moyou.weichat.bean.ChatVeryocalBean;
import com.moyou.weichat.listener.ChatMessageDeleteListener;
import com.moyou.weichat.utils.ChatUtils;
import com.moyou.weichat.utils.ConstantUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/17 0017.
 * sj: 2017年10月17日14:20:05
 * name : ts
 * 用途： 聊天的列表的adapter
 */

public class ChatVerticalListAdater extends BaseQuickAdapter<EMConversation,BaseViewHolder>{
    private ChatMessageDeleteListener chatMessageDeleteListener;

    public void setChatMessageDeleteListener(ChatMessageDeleteListener chatMessageDeleteListener) {
        this.chatMessageDeleteListener = chatMessageDeleteListener;
    }

    public ChatVerticalListAdater(@LayoutRes int layoutResId, @Nullable List<EMConversation> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, EMConversation item) {
        String userName = item.conversationId();
        SwipeMenuLayout swipeMenuLayout =  helper.getView(R.id.mSwipeMenuLayout);
        helper.setText(R.id.item_message_name,userName); //设置名字
        helper.setText(R.id.item_message_sig, ChatUtils.getChatUtils(mContext).getMessageDigest(item.getLastMessage(),mContext)); //设置获取到的会话信息
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(userName);
        EMMessage lastMessage = conversation.getLastMessage();
        if (conversation != null) {
            int messageNumber = conversation.getUnreadMsgCount(); //获取单条会话的未读消息条目数
            if ( messageNumber > ConstantUtils.TYPE_NUMBER ) {
                helper.setVisible(R.id.unread_address_number, true);
            } else {
                helper.setVisible(R.id.unread_address_number, false);
            }
            helper.setText(R.id.unread_address_number ,messageNumber+"" );
        }
        helper.setText(R.id.item_message_timer, DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
        helper.setOnClickListener(R.id.de_btn, view -> {
            swipeMenuLayout.quickClose(); //关闭侧滑
            chatMessageDeleteListener.onDeleteMessage(userName,helper.getAdapterPosition());
        });
    }
}
